﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neudesic.Pulse.Entities;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Neudesic.Pulse.Client.Tests
{
    /// <summary>
    /// Summary description for PulseClientTests
    /// </summary>
    [TestClass]
    public class RestResourceClientGetTests
    {

        private TestContext testContextInstance;
        PulseClient client = new PulseClient(TestHelper.BaseAddress, AuthenticationScheme.Ntlm);

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        [TestCategory("Integration")]
        public void RestResourceClient_CurrentUser()
        {

            Entity entity = client.GetCurrentUser();

            Assert.IsNotNull(entity);
        
        }

        [TestMethod]
        [TestCategory("Integration")]
        public void RestResourceClient_GetActivities()
        {

            Entity me = client.CurrentUser;
            PagedActivityList myStuff = client.GetPagedActivityList(me.Url);

            Assert.IsNotNull(myStuff);

        }

        [TestMethod]
        [TestCategory("Integration")]
        public void RestResourceClient_GetActivity()
        {

            Entity me = client.CurrentUser;
            PagedActivityList myStuff = client.GetPagedActivityList(me.Url);

            Activity activity = client.GetActivity(myStuff.Activities.First().Url);

            Assert.IsNotNull(activity);
            Assert.IsFalse(string.IsNullOrEmpty(activity.Text));

        }

        [TestMethod]
        [TestCategory("Integration")]
        public void RestResourceClient_GetHandle()
        {

            Entity me = client.CurrentUser;
            string handle = client.GetHandle(me);

            Assert.IsFalse(string.IsNullOrEmpty(handle));

        }


        [TestMethod]
        [TestCategory("Integration")]
        public void RestResourceClient_GetSubscriptions()
        {

            Entity me = client.CurrentUser;
            List<Entity> subscriptions = client.GetSubscriptions(me);

            Assert.IsNotNull(subscriptions);

        }

        [TestMethod]
        [TestCategory("Integration")]
        public void RestResourceClient_GetQuestionSuggestions()
        {

            List<Activity> suggestions = client.GetQuestionSuggestions("Where");

            Assert.IsNotNull(suggestions);

        }

        [TestMethod]
        [TestCategory("Integration")]
        public void RestResourceClient_GetPendingActivityCount()
        {

            Entity me = client.CurrentUser;
            PagedActivityList myStuff = client.GetPagedActivityList(me.Url);

            // Grab the newer items link
            string newer = myStuff.PagingBookmark.Newer;

            // Add an activity
            // Add the status so we can delete it later.
            StatusRequest entity = new StatusRequest()
            {
                Text = string.Format("Sample status message.  {0}", Guid.NewGuid().ToString()),
            };

            client.CreateStatus(entity, me);

            // Get the count and verify it's correct.
            int count = client.GetPendingActivityCount(newer);

            Assert.AreEqual<int>(1, count);

        }

        [TestMethod]
        [TestCategory("Integration")]
        public void RestResourceClient_GetFollowing_Users()
        {

            Entity me = client.CurrentUser;
            List<Entity> results = client.GetSubscriptions(me, EntityType.Person);

            Assert.IsNotNull(results);
            Assert.AreNotEqual<int>(0, results.Count);

        }

        [TestMethod]
        [TestCategory("Integration")]
        public void RestResourceClient_GetSearchResults()
        {

            List<Entity> results = client.Search<Entity>(EntityType.Person, "a");
            Assert.IsNotNull(results);

            results = client.Search<Entity>(EntityType.Person, "zzz");
            Assert.IsNotNull(results);

            results = client.Search<Entity>(EntityType.Page, "a");
            Assert.IsNotNull(results);

            results = client.Search<Entity>(EntityType.System, "a");
            Assert.IsNotNull(results);

            results = client.Search<Entity>(EntityType.Tag, "a");
            Assert.IsNotNull(results);

            List<Activity> results2 = client.Search<Activity>(EntityType.Phrase, "a");
            Assert.IsNotNull(results2);

        }
    }
}
